package priv.menghan.happy.learning.common.util;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 *
 */
public class BeanUtils {
    public static <T> List<T> copyArray(Collection sourceList, Class<T> clz) throws Exception {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Collections.EMPTY_LIST;
        }
        List<T> list = new ArrayList<>(sourceList.size());
        for (Object source : sourceList) {
            T target = null;
            target = clz.newInstance();
            org.springframework.beans.BeanUtils.copyProperties(source, target);
            list.add(target);
        }
        return list;
    }

    /**
     * 功能描述 复制bean
     *
     * @param source 源对象
     * @param clz    目标对象类型
     * @return T
     * @auther 吞星（yangguojun）
     * @date 2020/3/17-18:38
     */
    public static <T> T copyBean(Object source, Class<T> clz) throws Exception {
        T target = null;
        target = clz.newInstance();
        org.springframework.beans.BeanUtils.copyProperties(source, target, null, null);
        return target;
    }
}